package au.edu.swin.swinvehicle.bookingtype;

import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * BookingType entity
 *
 * @version 1.0
 * @author Dany Gunawan
 * @lastmodify 2009-05-12
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "findAllBookingTypes",
    query = "SELECT bt FROM BookingTypeEntity bt")
})
@Table(name = "BOOKINGTYPE")
public class BookingTypeEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(length = 15, unique = true, nullable = false)
    private String code;
    @Column(length = 255, nullable = false)
    private String description;
    @OneToMany(mappedBy = "bookingType", cascade = CascadeType.ALL)
    private List<VehicleBookingEntity> vehicleBookings;

    /**
     * Default constructor
     */
    public BookingTypeEntity() throws BookingTypeManagerException {
        try {
            vehicleBookings = new ArrayList<VehicleBookingEntity>();
        } catch (Exception e) {
            throw new BookingTypeManagerException("Cannot instantiate new BookingTypeEntity object", e);
        }
    }

    public BookingTypeEntity(String code, String description) throws BookingTypeManagerException {

        this(); // Call default constructor
        try {
            setCode(code);
            setDescription(description);
        } catch (Exception e) {
            throw new BookingTypeManagerException("Cannot instantiate new BookingTypeEntity object", e);
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<VehicleBookingEntity> getVehicleBookings() {
        return vehicleBookings;
    }

    public void setVehicleBookings(List<VehicleBookingEntity> vehicleBookings) {
        this.vehicleBookings = vehicleBookings;
    }

    // Transfer Object Methods
    public BookingTypeTO getData() throws BookingTypeManagerException {
        BookingTypeTO to = null;
        try {
            to = new BookingTypeTO(id, code);
        } catch (Exception e) {
            throw new BookingTypeManagerException("Cannot get BookingTypeTO data", e);
        }
        return to;
    }

    public BookingTypeDetailedTO getDetailedData() throws BookingTypeManagerException {
        BookingTypeDetailedTO to = null;

        try {
            to = new BookingTypeDetailedTO(id, code, description);
        } catch (Exception e) {
            throw new BookingTypeManagerException("Cannot get BookingTypeDetailedTO data", e);
        }
        return to;
    }

    // Only the BookingTypeDetailedTO can be used to update the state of the booking-type entity
    public void setData(BookingTypeDetailedTO to) throws BookingTypeManagerException {
        try {
            setId(to.getId());
            setCode(to.getCode());
            setDescription(to.getDescription());
        } catch (Exception e) {
            throw new BookingTypeManagerException("Cannot set BookingTypeDetailedTO data", e);
        }
    }

    @Override
    public String toString() {
        return "au.edu.swin.swinvehicle.bookingtype.BookingTypeEntity[id=" + id + "]";
    }
}
